<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公告标题">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接收者">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入操作人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公告类型">
        <el-select v-model="queryParams.msgType" multiple placeholder="公告类型" clearable>
          <el-option
            v-for="dict in dict.type.message_type"
            :key="dict.value"
            :label="dict.label"
            :value="+dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接收者类型" label-width="90px">
        <el-select v-model="queryParams.type" placeholder="接收者类型">
          <el-option
            v-for="dict in dict.type.notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="+dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleUpdate(undefined)"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="ids.length === 0"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="index" width="50" />
      <el-table-column
        label="公告标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="公告类型" align="center" prop="msgType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.message_type" :value="scope.row.msgType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_notice_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="接收者" align="center" prop="receiver" width="120" />
      <el-table-column label="创建者" align="center" prop="createBy" width="120" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="200" />
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="row.status === 0"
            @click="handleRelease(row)"
          >发布</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <Add ref="add" :sys_notice_status="dict.type.sys_notice_status" :sys_notice_type="dict.type.sys_notice_type" :message_type="dict.type.message_type" :notice_type="dict.type.notice_type" @close="getList" />
  </div>
</template>

<script>
import { getNotice, delNotice, addNotice, updateNotice, releaseNotice } from '@/api/system/notice'
import { getMessageAddList } from '@/api/message'
import Add from '@/views/system/notice/components/add.vue'

export default {
  name: "Notice",
  components: { Add },
  dicts: ['sys_notice_status', 'message_type', 'notice_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 9999, // 接收者类型
        title: undefined,
        msgType: undefined,
        receiver: undefined
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      getMessageAddList(this.queryParams).then(response => {
        response.rows.forEach((item, index) => {
          item.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + (index + 1)
        })
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
    },
    /** 新增按钮操作 */
    handleUpdate(row) {
      console.log(row, this.$refs.add)
      this.$refs.add.open(row)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.id || this.ids.join(',')
      this.$modal.confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(function() {
        return delNotice({ ids: noticeIds });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    // 发布
    handleRelease(row) {
      this.$confirm('是否发布该公告', '提示').then(res => {
        this.loading = true
        releaseNotice({
          id: row.id
        }).then(res => {
          this.loading = false
          this.$bus.emit('read')
          this.getList();
          this.$message.success("发布成功");
        }).catch(() => {
          this.loading = false
          this.$message.error("发布失败");
        });
      })
    }
  }
};
</script>
